package org.example.controller;

import org.example.model.Book;
import org.example.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;

    //获取图书信息（数据字典：获取下拉菜单选项）
    //GET /api/book/queryAsDict
    @RequestMapping("/queryAsDict")
    public Object queryAsDict(){
        List<Book> books = bookService.queryAsDict();
        return books;
    }

    //获取图书列表（表格展示）
    //GET /api/book/query
    @RequestMapping("/query")
    public Object query() {
        List<Book> books = bookService.query();
        return books;
    }

    //新增图书
    //POST /api/book/add
    //{"bookName":"十三点","author":"","price":""}
    @RequestMapping("/add")
    public Object add(@RequestBody Book book){
        int n = bookService.add(book);
        return null;
    }

    //获取图书详情（修改图书时，先查询）
    //GET /api/book/queryById?id=xxx
    @RequestMapping("/queryById")
    public Object queryById(Integer id){
        Book book = bookService.queryById(id);
        return book;
    }

    //修改图书
    //POST /api/book/update
    //{"id":1,"bookName":"十三点","author":"","price":""}
    @RequestMapping("/update")
    public Object update(@RequestBody Book book){
        int n = bookService.update(book);
        return null;
    }
    //删除图书（一个或多个）
    //GET /api/book/delete?ids=xxx&ids=xxx
    @RequestMapping("/delete")
    public Object delete(@RequestParam List<Integer> ids){
        int n = bookService.delete(ids);
        return null;
    }
}
